package com.sys.gradepaysys.service;

import com.sys.gradepaysys.dao.BaseDao;
import com.sys.gradepaysys.entity.Event;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EventServiceImpl implements EventService{
    @Override
    public boolean addEvent(Event event) {
        String sql="INSERT INTO event (e_pay, e_name, e_num, s_date, e_date, is_ok, is_doing, sup_num, no_sup, c_id) VALUES ( ?, ?, ?, ?, ?, 0, 0, 0, 0, ?)";
        String arr[]={event.getePay().toString(),event.geteName(),event.geteNum().toString(),event.getsTime(),event.geteTime(),event.getCid().toString()};
        boolean b = BaseDao.DoObject(sql, arr);
        return b;
    }

    @Override
    public List<Event> getEventListIsDoing() throws ParseException {
        String sql="select * from event where is_doing =1";
        List<Event> events = BaseDao.selectEventIsDoingList(sql);
        return events;
    }

    @Override
    public Event getEventByEid(Integer eid) throws ParseException {
        String sql="select * from event where e_id =?";
        String arr[]={eid.toString()};
        List<Event> events = BaseDao.selectEventIsDoingList(sql,arr);
        Event event=new Event();
        for (Event event1 : events) {
             event=event1;
        }
        return event;
    }

    @Override
    public boolean supportEvent(Integer eid,String support) throws ParseException {
        String arr[]={eid.toString()};
        Event eventByEid = this.getEventByEid(eid);
        Integer supNum = eventByEid.getSupNum();
        Integer noSup = eventByEid.getNoSup();
        noSup+=1;
        supNum+=1 ;
        String sql="";
        boolean b=false;
        if (support.equals("support")){
            sql="update event set sup_num=? where e_id=?";
            String arr2[]={supNum.toString(),eid.toString()};
             b = BaseDao.DoObject(sql, arr2);
        }
        if (support.equals("no")){
            sql="update event set no_sup=? where e_id=?";
            String arr2[]={noSup.toString(),eid.toString()};
            b = BaseDao.DoObject(sql, arr2);
        }
        return b;
    }

    @Override
    public boolean supportStart(Integer eid) {
        String sql="update event set is_doing=1 where e_id=?";
        String arr2[]={eid.toString()};
        boolean b = BaseDao.DoObject(sql, arr2);
        return b;
    }

    @Override
    public boolean checkStuEventSupport(Integer sid, Integer eid) {
        String sql="select * from s_stu_e where sid=? and eid=?";
        String arr[]={sid.toString(),eid.toString()};
        List<Map<String, String>> maps = BaseDao.selectList(sql, arr);
        if (maps.size()>0){
            return  false;
        }else {
            sql="insert into s_stu_e (sid,eid) values(?,?)";
            String arr2[]={sid.toString(),eid.toString()};
            boolean b = BaseDao.DoObject(sql, arr2);
        }
        return true;
    }

    @Override
    public List<Event> getEventListPay() throws ParseException {
        String s = LocalDateTime.now().toString();
        String sql="select * from `event` where e_date < ?";
        String arr[]={s};
        List<Event> events = BaseDao.selectEventIsDoingList(sql, arr);
        List<Event> eventList=new ArrayList<>();
        for (Event event : events) {
            Integer total=  event.getSupNum()+event.getNoSup();
            if (total!=0){
                Double isSuccess= Double.valueOf(event.getSupNum()/total);
                if (isSuccess>(2/3)){
                    eventList.add(event);
                }
            }
        }
        for (Event event : eventList) {
            String sql1="update event set is_ok=1 where e_id=?";
            String arr1[]={event.getEid().toString()};
            BaseDao.DoObject(sql1,arr1);
        }
        String sql2="select * from `event` where e_date < ? and is_ok=1";
        List<Event> events2 = BaseDao.selectEventIsDoingList(sql2, arr);
        return events2;
    }

    @Override
    public boolean StuToPay(String sid, String eid,String pay) throws ParseException {
        String arr[]={sid,eid,pay,LocalDateTime.now().toString()};
        String sql="insert into spay(sid,eid,pay,pdate)values(?,?,?,?)";
        Double total=0.0;
        Double aDouble = Double.valueOf(pay);
        String  sql3="select * from event where e_id=?";
        String arr3[]={eid};
        List<Event> events = BaseDao.selectEventIsDoingList(sql3, arr3);
        Event event2 =new Event();
        for (Event event : events) {
            event2=event;
        }
        Double totalPay = event2.getTotalPay();
        total=totalPay+aDouble;
        String sql2="update event set e_total=? where e_id=?";
        String arr2[]={total.toString(),eid};
        BaseDao.DoObject(sql2,arr2);
        boolean b = BaseDao.DoObject(sql, arr);
        return b;
    }

    @Override
    public List<Event> getEventListIsOk() throws ParseException {
        String sql="select * from event where is_ok=1";
        List<Event> events = BaseDao.selectEventIsDoingList(sql);
        return events;
    }
}
